import { Engine } from "../../../Engine";
import { Base } from "../../../core/Base";
import { Pass } from "../Pass";
/**
 * 后处理效果类基类。
 * @class
 * @memberof w.effects
 */
class Effect extends Base {
  /**
   * @constructor
   * @param {*} opts 
   */
  constructor(opts = {}) {
    super(opts);
    this._renderToScreen = false;
    this._computes = [];
    this._enable=true;
  }
  set enable(v){
    if(this.enable!==v){
      this._enable=v;
      if(this.watchers[0]){
        this.watchers[0].rebuild();
      }
    }
    return this;
  }
  get enable(){
    return this._enable;
  }
  /**
   * @property {Array<w.compute.Compute>} 获取所有的计算着色器。 
   */
  get computes() {
    return this._computes;
  }
  get renderToScreen() {
    return this._renderToScreen;
  }
  /**
   * @property {Boolean} 是否渲染到屏幕,通常在添加后处理的过程中，最后一个后处理会渲染到屏幕，其它则渲染到纹理。
   */
  set renderToScreen(v) {
    this._renderToScreen = v;
    return this;
  }
  set type(v){
    this._type=v;
    return this;
  }
  get type(){
    return this._type;
  }
  /**
   * 添加计算着色器。
   * @method
   * @param {w.compute.Compute} compute GPU计算类。
   */
  addCompute(compute) {
    if (!this.hasComputer(compute)) {
      compute.bind(this);
      this.computes.push(compute);
    }
    return this;
  }
  hasComputer(compute) {
    const isExist = this.computes.indexOf(compute);
    if (isExist == -1) {
      return false;
    } else {
      true;
    }
  }
  removeCompute(compute) {
    const id = getId(compute);
    for (let i = this.computes.length - 1; i >= 0; i--) {
      if (this.computes[i].id === id) {
        this.splice(i, 1);
        compute.unBind(this);
      }
    }
    return this;
  }
  onRender(commandEncoder,last = true) {
    //子类实现
    Pass.instance().swap();
    for (let i = 0; i < this.computes.length; i++) {
      this.computes[i].onCompute(commandEncoder);
    }
    let pass = this.getDescript();
    if (this.renderToScreen && last) {
      //如果渲染到屏幕，则绑定到屏上。
      pass.colorAttachments[0].view = Engine.instance.context.getCurrentTexture().createView();
    }
    return pass;
  }
  update() {
    this.notice();
  }
}
const EffectType = {
  Blur: 0,
  ColorAdjust: 1,
  Fxaa: 2,
  GlobalFog: 3,
  GTAO:4,
  LGGO:5,
  Sharp:6,
  TAA:7,
  Vignette:8

};
export { Effect, EffectType };
